home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / fapxtool / src / txl / txldir.c < prev    next >
C/C++ Source or Header  |  1995-02-14  |  11KB  |  480 lines

  1. /***************
  2. *
  3. * g:\exe\txf\src\txldir.c
  4. */
  5. #include "txl.h"
  6.  
  7. #define SORT_DATE    1
  8. #define SORT_SIZE    2
  9. #define SORT_NAME    3
  10. #define SORT_EXT    4
  11. #define SORT_FAPX    5
  12. #define PRI_NUM        28
  13.  
  14. extern int (*qsort_func_tbl[6])(struct find_t_s *x, struct find_t_s *y);
  15. extern int dirsortoption;
  16.  
  17. #if 0        /* 自己書換え版 */
  18. char *castlow(char *str)
  19. {
  20.     int i;
  21.     if (castname) {
  22.         for (i = 0; *(str + i) != 0; i++) {
  23.             *(str + i) = tolower(*(str + i));
  24.         }
  25.     }
  26.     return (str);
  27.  
  28. }
  29. #else
  30. char *castlow1(char *str)
  31. {
  32.     static char low[80];
  33.     static int old = 0;
  34.     int i;
  35.     old = 16 - old;
  36.     if (castname) {
  37.         for (i = 0; *(str + i) != 0; i++) {
  38.             *(low + i + old) = tolower(*(str + i));
  39.         }
  40.         *(low + i + old) = NUL;
  41.         return (low + old);
  42.     }
  43.     else {
  44.         return (str);
  45.     }
  46.  
  47. }
  48. char *castlow2(char *str)
  49. {
  50.     static char low[80];
  51.     static int old = 0;
  52.     int i;
  53.     old = 16 - old;
  54.     if (castname) {
  55.         for (i = 0; *(str + i) != 0; i++) {
  56.             *(low + i + old) = tolower(*(str + i));
  57.         }
  58.         *(low + i + old) = NUL;
  59.         return (low + old);
  60.     }
  61.     else {
  62.         return (str);
  63.     }
  64.  
  65. }
  66. #endif
  67.  
  68. void subdir(struct find_t_s *fcbs, FILE *fp, char *name, int base)
  69. {
  70.     int i = 0, j, len = 0, other;
  71.     struct find_t newfcb;    /* 43バイト    */
  72.     char *ext;
  73. /*    int year, month, day, hour, minute, second;    */
  74.  
  75.     strcpy(line1, inputfile);
  76.     ext = line1 + strlen(line1) - 3;
  77.     strcpy(ext, name);
  78.     strcat(ext, "\\*.*");
  79.     if (_dos_findfirst(line1, _A_SUBDIR, &newfcb) == 0) {
  80.         do {
  81.             if ((*(newfcb.name) != '.') && (newfcb.attrib & _A_SUBDIR)) {
  82.                 fcbs[base + i].attrib = newfcb.attrib;
  83.                 fcbs[base + i].wr_time = newfcb.wr_time;
  84.                 fcbs[base + i].wr_date = newfcb.wr_date;
  85.                 fcbs[base + i].size = newfcb.size;
  86.                 strcpy(fcbs[base + i].name, newfcb.name);
  87.                 i++;
  88.                 if (base + i >= maxfcbs) {
  89.                     break;
  90.                 }
  91.             }
  92.         } while (_dos_findnext(&newfcb) == 0);
  93.     }
  94.     if (i > 1) {
  95.         if (sortoption != 0) {
  96.             qsort(&(fcbs[base]), i, sizeof(struct find_t_s),
  97.                     qsort_func_tbl[abs(sortoption)]);
  98.         }
  99.     }
  100.     for (j = 0; j < i; j++) {
  101.         len = strlen(line3);
  102.         if (name != line3) {
  103.             sprintf(line3, "%s\\%s", name, fcbs[base+j].name);
  104.         }
  105.         /* 空いてる変数は何でも使う(^^;) */
  106.         fprintf(fp, "%s\\%-*s    <DIR>  ", castlow1(name),
  107.                 21 - strlen(name), castlow2(fcbs[base+j].name));
  108.         if (name == line3) {
  109.             strcat(line3, "\\");
  110.             strcat(line3, fcbs[base+j].name);
  111.         }
  112.         other = 1;
  113.         if (dspforumname) {
  114.             other = dspmyforum(line3, fp);
  115.         }
  116.         if (other) {
  117.             fputc('\n', fp);
  118.         }
  119.         subdir(fcbs, fp, line3, base + i + 1);
  120.         *(line3 + len) = NUL;
  121.     }
  122. }
  123.  
  124. int serchdir(struct find_t_s *fcbs, unsigned int attr, long *para)
  125. {
  126.     struct find_t newfcb;    /* 43バイト    */
  127.     int i = 0, j, entry;
  128.     char *ext;
  129.  
  130.     if (_dos_findfirst(inputfile, attr, &newfcb) == 0) {
  131.         do {
  132.             if (newfcb.attrib & _A_SUBDIR) {
  133.                 if (*(newfcb.name) != '.') {
  134.                     fcbs[i].attrib = newfcb.attrib;
  135.                     fcbs[i].wr_time = newfcb.wr_time;
  136.                     fcbs[i].wr_date = newfcb.wr_date;
  137.                     fcbs[i].size = newfcb.size;
  138.                     strcpy(fcbs[i].name, newfcb.name);
  139.                     i++;
  140.                     if (i >= maxfcbs) {
  141.                         break;
  142.                     }
  143.                 }
  144.             }
  145.             else {
  146.                 entry = 1;
  147.                 for (j = 0; j < maxexclude; j++) {
  148.                     if ((ext = jstrchr(newfcb.name, '.')) != NULL) {
  149.                         if (far_strncmp((char *)(&(para[j])), ext, 4) == 0) {
  150.                             entry = 0;
  151.                             break;
  152.                         }
  153.                     }
  154.                 }
  155.                 if (entry) {
  156.                     fcbs[i].attrib = newfcb.attrib;
  157.                     fcbs[i].wr_time = newfcb.wr_time;
  158.                     fcbs[i].wr_date = newfcb.wr_date;
  159.                     fcbs[i].size = newfcb.size;
  160.                     strcpy(fcbs[i].name, newfcb.name);
  161.                     i++;
  162.                     if (i >= maxfcbs) {
  163.                         break;
  164.                     }
  165.                 }
  166.             }
  167.         } while (_dos_findnext(&newfcb) == 0);
  168.     }
  169.     if (i > 1) {
  170.         if (sortoption != 0) {
  171.             qsort(fcbs, i, sizeof(struct find_t_s), qsort_func_tbl[abs(sortoption)]);
  172.         }
  173.     }
  174.     return (i);
  175. }
  176.  
  177. void dirmain(struct find_t_s *fcbs, FILE *fp, unsigned int attr, long *para)
  178. {
  179.     int i, j, other;
  180.     PRIORITY *pri;
  181.     char *ext;
  182.     int year, month, day, hour, minute, second, timestamp = 1;
  183.     char fname[13];
  184.  
  185.     i = serchdir(fcbs, attr, para);
  186.  
  187.     for (j = 0; j < i; j++) {    /* 表示    */
  188.         strcpy(fname, fcbs[j].name);
  189.         if (dspfile) {
  190.             if ((ext = jstrchr(fname, '.')) != NULL) {
  191.                 *ext = NUL;
  192.                 ext++;
  193.             }
  194.             else {
  195.                 ext = strchr(fname, '\0');    /* ダミー    */
  196.             }
  197.         }
  198.         if (fcbs[j].attrib & _A_SUBDIR) {
  199.             if (dspfile) {
  200.                 fprintf(fp, "%-8s %-3s <DIR>     ",
  201.                         castlow1(fname), castlow2(ext));
  202.             }
  203.             else {
  204.                 fprintf(fp, "%-12s <DIR>     ", castlow1(fname));
  205.             }
  206.         }
  207.         else {
  208.             if (dspfile) {
  209.                 fprintf(fp, "%-8s %-3s %8ld  ",
  210.                         castlow1(fname), castlow2(ext), fcbs[j].size);
  211.             }
  212.             else {
  213.                 fprintf(fp, "%-12s %8ld  ",
  214.                         castlow1(fname), fcbs[j].size);
  215.             }
  216.         }
  217.         year = (fcbs[j].wr_date >> 9) + 80;
  218.         month = (fcbs[j].wr_date >> 5) & 0x0f;
  219.         day = fcbs[j].wr_date &0x1f;
  220.         fprintf(fp, "%02d-%02d-%02d  ", (year) % 100, month, day);
  221.         if (dirinfo) {
  222.             timestamp = 0;
  223. /*            fprintf(stderr, "%s\n", fcbs[j].name);    */
  224.             if (matchstr("%%%%%%%%.LOG", fcbs[j].name)) {    /* NORMAL LOG */
  225.                 timestamp = 1;
  226.             }
  227.             else if (matchstr("***%%%%%.ML%", fcbs[j].name)) {/* CATLOG MAIL */
  228.                 catlogmail(fcbs[j].name, fp);
  229.             }
  230.             else if (matchstr("MES%%'_%%.TXT", fcbs[j].name)) {/* CATLOG LOG */
  231.                 catlogmes(fcbs[j].name, fp);
  232.             }
  233.             else if (matchstr("MES%%.LOG", fcbs[j].name)) {/* MES_MODE LOG */
  234.                 catlogmes(fcbs[j].name, fp);
  235.             }
  236.             else if (matchstr("LIB%%.TXT", fcbs[j].name)) {    /* CATLOG LIB */
  237.                 catloglib(fcbs[j].name, fp);
  238.             }
  239.             else if (strstr(fcbs[j].name, ".RTN") != NULL) {    /* RTN LIST */
  240.                 rtnmes(fcbs[j].name, fp);
  241.             }
  242.             else if (strstr(fcbs[j].name, ".LST") != NULL) {    /* LIB LIST */
  243.                 liblist(fcbs[j].name, fp);
  244.             }
  245.             else if (matchstr("***%%%%%.LOG", fcbs[j].name)) {/* HP/PATIO LOG*/
  246.                 idlog(fcbs[j].name, fp);
  247.             }
  248.             else {
  249.                 pri = howpri(&(fcbs[j]));
  250.                 other = 1;
  251.                 if (dspforumname) {
  252.                     other = dspmyforum(fcbs[j].name, fp);
  253.                 }
  254.                 if ((other) && ((~(pri->pri)) & 0x0100)) {
  255.                     fputs(pri->str, fp);
  256.                     fputc('\n', fp);
  257.                 }
  258.                 else if (other) {
  259.                     fputc('\n', fp);
  260.                 }
  261.             }
  262. /*            fprintf(stderr, "(%d)%s\n", pri->pri, pri->str);    */
  263.         }
  264.         if (timestamp) {
  265.             hour = fcbs[j].wr_time>>11;
  266.             minute = (fcbs[j].wr_time>>5) & 0x3f;
  267.             second = (fcbs[j].wr_time & 0x1f)<<1;
  268.             fprintf(fp, "%2d:%02d:%02d\n", hour, minute, second);
  269. #if 0    /* 何をしていたのか良く分からない(^^;)---1994.04.10 */
  270.             if (abs(sortoption) == SORT_FAPX) {
  271.                 pri = howpri(&(fcbs[j]));
  272.             }
  273.             else {
  274.                 pri = &(kindofpri[PRI_NUM - 1]);    /* ダミー */
  275.             }
  276. #endif
  277.         }
  278.         if ((fcbs[j].attrib & _A_SUBDIR) && (recursivedir)) {
  279.             for (other = 0; -1<= finfo[other].key; other = finfo[other].next) {
  280.                 if (finfo[other].key == -1) {
  281.                     if (far_strcmp(finfo[other].fname, fcbs[j].name) == 0) {
  282.                         subdir(fcbs, fp, fcbs[j].name, i);
  283.                     }
  284.                 }
  285.                 if (finfo[other].next == 0) {
  286.                     break;
  287.                 }
  288.             }
  289.         }
  290.     }
  291. }
  292.  
  293. void dir(int dirsort, int filesort)
  294. {
  295.     int i;
  296.     struct find_t_s *fcbs;    /* find_t配列へのポインタ    */
  297.     FILE *fp = NULL;
  298.     char *myforum = "MYFORUM.LOG";
  299.     char *dirend;
  300.     long *para;
  301.  
  302.     if (outputfile != NULL) {
  303.         fp = fopen(outputfile, "w");
  304. /*        fprintf(stdout, "writefile = %s(%d)\n", outputfile, fp);    */
  305.     }
  306.     else {
  307.         fp = fpmes;
  308.     }
  309.     if (fp == NULL) {
  310.         Exit(1);
  311. /*        fp = stdout;    */
  312. /*        puts("outout stdout");    */
  313.     }
  314.  
  315.     para = malloc(3000*4);
  316.     if (para == NULL) {
  317.         goto ERROR;
  318.     }
  319.  
  320.     strcpy(line2, inputfile);
  321.     if (matchstr("*:\\", inputfile) && *(inputfile +3) != NUL) {
  322.         if (*(jstrrchr(inputfile, '\\') + 1) != NUL) {
  323.             strcat(line2, "\\");
  324.             strcat(inputfile, "\\");
  325.         }
  326.     }
  327.     strcat(inputfile, "*.*");
  328.  
  329.     for (i = 1024; (fcbs = malloc(i * sizeof(struct find_t_s))) == NULL; i--) {
  330.         if (i <= 0) {
  331.             goto ERROR;
  332.         }
  333.     }
  334.     maxfcbs = i;
  335. /*    if (dspforumname) {    */
  336. /*    fprintf(stdout, "fcbs(subdir)=%d\n", i);    @@@debug    */
  337.     for (i = 512; (finfo = farmalloc(i * sizeof(struct _foruminfo))) == NULL;
  338.             i -= 4) {
  339.         if (i <= 0) {
  340.             goto ERROR;
  341.         }
  342.     }
  343.     maxfinfo = i;
  344.     maxfinfodata = 0;
  345.     finfo[0].key = 0x7fff;
  346.     finfo[0].next = 0;
  347.     finfo[0].fname[0] = NUL;
  348.     finfo[0].title[0] = NUL;
  349.  
  350.     dirend = strchr(line2, NUL);
  351.     strcpy(dirend, myforum);
  352. #if 0
  353.     strcpy(dirend, title);
  354.     readtitle(line2);
  355. #endif
  356.     readtitle(txfsetfile, para);
  357.  
  358.     readmyforum(line2);
  359. #if 0    /* 雅びさんの要望により削除 */
  360.     if (finfo[0].next == 0) {
  361.         dspforumname = 0;
  362.     }
  363. #endif
  364. /*    }    */
  365.  
  366.     dirsortoption = dirsort;
  367.     sortoption = filesort;
  368.     dirmain(fcbs, fp, _A_SUBDIR | _A_NORMAL | _A_ARCH | _A_RDONLY, para);
  369.  
  370.     if (fp != fpmes) {
  371.         fclose(fp);
  372.     }
  373.  
  374.     farfree(finfo);
  375.     free(fcbs);
  376.     free(para);
  377.     return;
  378. ERROR:
  379.     fprintf(fp, "Error: out of memory(dir)\n");
  380.     if (fp != fpmes) {
  381.         fclose(fp);
  382.     }
  383.     Exit(1);
  384. }
  385.  
  386. void dirdriver(char *val[])
  387. {
  388.     int i = 0;
  389.     int sortswitch = 0;
  390.     int dirsort = 0;
  391.     int filesort = 0;
  392.     char inputpath[80];    /* うーん、inputfileを操作しているとは思わなかった */
  393.  
  394.     for (i = 0; val[i] != NULL; i++) {
  395.         if (val[i][0] == '-') {
  396.             switch (val[i][1]) {
  397.             case 'C':
  398.                 castname = 0;
  399.                 break;
  400.             case 'c':
  401.                 castname = 1;
  402.                 break;
  403.             case 'i':
  404.             case 'I':
  405.                 dirinfo = 1;
  406.                 break;
  407.             case 'o':
  408.             case 'O':
  409.                 if (val[i+1][0] != '-') {
  410.                     outputfile = val[i+1];
  411.                     i++;
  412.                 }
  413.                 break;
  414.             case 'd':
  415.             case 'D':
  416.                 sortoption = SORT_DATE;
  417.                 sortswitch = 1;
  418.                 break;
  419.             case 's':
  420.             case 'S':
  421.                 sortoption = SORT_SIZE;
  422.                 sortswitch = 1;
  423.                 break;
  424.             case 'n':
  425.             case 'N':
  426.                 sortoption = SORT_NAME;
  427.                 sortswitch = 1;
  428.                 break;
  429.             case 'e':
  430.             case 'E':
  431.                 sortoption = SORT_EXT;
  432.                 sortswitch = 1;
  433.                 break;
  434.             case 'x':
  435.             case 'X':
  436.                 sortoption = SORT_FAPX;
  437.                 sortswitch = 1;
  438.                 break;
  439.             case 'f':
  440.                 dspfile = 1;
  441.                 break;
  442.             case 'r':
  443.             case 'R':
  444.                 recursivedir = 1;
  445.                 break;
  446.             case 'F':
  447.                 dspforumname = 1;
  448.                 break;
  449.             default:
  450. /*                fprintf(stderr, "Error:invailed parameter'%Fs'\n", val[i]);    */
  451.                 Exit(1);
  452.             }
  453.         }
  454.         else if (inputfile == NULL) {
  455.             strcpy(inputpath, val[i]);
  456.             inputfile = inputpath;        /* inputfileをメモリ確保しているものと */
  457.         }                                /* して処理していたようなので逃げる */
  458.  
  459. #if 0    /* ここを無効にするとオプション無しの2番目引数が無効になる */
  460.         else if (inputfile == NULL) {
  461.             outputfile = val[i];
  462.         }
  463. #endif
  464.  
  465.         if (sortswitch) {
  466.             dirsort = sortoption;
  467.             filesort = sortoption;
  468.             if (val[i][2] != '-') {
  469.                 dirsort = -(sortoption);
  470.             }
  471.             if (val[i][3] != '-') {
  472.                 filesort = -(sortoption);
  473.             }
  474.             sortswitch = 0;
  475.         }
  476.     }
  477.     dir(dirsort, filesort);
  478.     Exit(0);
  479. }
  480.